package com.lzj.admin.controller;


import com.lzj.admin.dto.TreeDto;
import com.lzj.admin.model.RespBean;
import com.lzj.admin.model.RespDataBean;
import com.lzj.admin.pojo.Goods;
import com.lzj.admin.pojo.GoodsType;
import com.lzj.admin.service.IGoodsTypeService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品类别表 前端控制器
 * </p>
 *
 * @author luoyoumou
 * @date 2022-12-05
 * @version 1.0
 */
@Controller
@RequestMapping("/goodsType")
public class GoodsTypeController {

    @Resource
    private IGoodsTypeService goodsTypeService;

    /**
     * 返回所有商品分类数据
     * @return
     */
    @RequestMapping("queryAllGoodsTypes")
    @ResponseBody
    public List<TreeDto> queryAllGoodsTypes(Integer typeId){
        return goodsTypeService.queryAllGoodsTypes(typeId);
    }

    @RequestMapping("index")
    public String index(){
        return "goodsType/goods_type";
    }

    @RequestMapping("list")
    @ResponseBody
    public RespDataBean<GoodsType> goodsTypeList()
    {
        return goodsTypeService.goodsTypeList();
    }

    @RequestMapping("addOrUpdateGoodsTypePage")
    public String addOrUpdateGoodsTypePage(Integer id, Integer pId, Model model){
        if(null != pId){
            // 添加页面
            model.addAttribute("pId",pId);
        } else if ( null != id){
            // 更新页面
            model.addAttribute("id",id);
            model.addAttribute("goodsType",goodsTypeService.getById(id));
        } else {
            System.out.println("addOrUpdateGoodsTypePage 输入参数有误，请检查！");
        }
        return "goodsType/add_update";
    }

    @RequestMapping("save")
    @ResponseBody
    public RespBean saveGoodsType(GoodsType goodsType){
        //添加分类
        goodsTypeService.saveGoodsType(goodsType);

        return RespBean.success("商品类别记录添加成功");
    }

    /**
     * 更新商品接口
     * @param goodsType
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public RespBean updateGoodsType(GoodsType goodsType){
        goodsTypeService.updateGoodsType(goodsType);
        return RespBean.success("商品类别记录更新成功！");
    }

    @RequestMapping("delete")
    @ResponseBody
    public RespBean deleteGoodsType(Integer id){
        goodsTypeService.deleteGoodsType(id);
        return RespBean.success("商品类别记录删除成功");
    }

}
